package org.jash.learnmvp.user;

import org.jash.learnmvp.mvp.BasePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;

public class CodeLoginPresenter extends BasePresenter<ConStruct.ICodeLoginView> implements ConStruct.ICodeLoginPresenter {
    private ConStruct.ICodeLoginModel model = new CodeLoginModel();
    @Override
    public void loadCode(String phone) {
        disposable.add(model.getLCode(phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (isAttach()) {
                        if (res.getCode() == 0) {
                            getWhoView().codeSuccess(res.getData());
                        } else {
                            getWhoView().failure(new RuntimeException(res.getMsg()));
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }

    @Override
    public void login(String phone, String code) {
        disposable.add(model.login(phone, code)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (isAttach()) {
                        if (res.getCode() == 0) {
                            getWhoView().loginSuccess(res.getData());
                        } else {
                            getWhoView().failure(new RuntimeException(res.getMsg()));
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));

    }

    @Override
    public void logout() {
        disposable.add(model.logout()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (isAttach()) {
                        if (res.getCode() == 0) {
                            getWhoView().logoutSuccess();
                        } else {
                            getWhoView().failure(new RuntimeException(res.getMsg()));
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }
}
